package com.example.sort;


import java.util.Stack;

public class TreeNode {

    TreeNode left;
    TreeNode right;
    int val;

    public TreeNode(int val) {
        this.val = val;
    }

    public void preOrder(TreeNode treeNode) {
        if (treeNode == null) {
            return;
        } else {
            System.out.println(treeNode.val);
            preOrder(treeNode.left);
            preOrder(treeNode.right);
        }

        if (treeNode != null) {
            System.out.println(treeNode.val);
            preOrder(treeNode.left);
            preOrder(treeNode.right);
        }


        //非递归
        Stack<TreeNode> stack = new Stack<>();
        if (treeNode == null) return;

        stack.push(treeNode);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            System.out.println(node.val);

            /**
             *
             * 这样时不对的  应该先把右孩子放进栈 这样左孩子才能先出栈
             * if (node.left!=null){
             *                 stack.push(node.left);
             *             }
             *             if (node.right!=null){
             *                 stack.push(node.right);
             *             }
             * */

            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }

        }


        TreeNode p = treeNode;
        while (p != null || !stack.empty()) {
            if (p != null) {
                System.out.println(p.val);
                stack.push(p);
                p = p.left;
            } else {
                if (!stack.isEmpty()) {
                    p = stack.pop();
                    p = p.right;
                }
            }

        }
    }

    public void inorder(TreeNode treeNode) {
        if (treeNode != null) {
            inorder(treeNode.left);
            System.out.println(treeNode.val);
            inorder(treeNode.right);
        }

        if (treeNode == null) return;
        Stack<TreeNode> stack = new Stack<>();
//        TreeNode node = treeNode;
        while (treeNode != null || !stack.isEmpty()) {
            while (treeNode != null) {
                stack.push(treeNode);
                treeNode = treeNode.left;
            }
            if (!stack.empty()) {
//                TreeNode temp = stack.pop();
//                System.out.println(temp.val);
//                node = node.right;
                treeNode = stack.pop();
                System.out.println(treeNode.val);
                treeNode = treeNode.right;
            }
        }
    }

    //后序遍历
    public void postOrder(TreeNode treeNode) {
        if (treeNode != null) {
            postOrder(treeNode.left);
            postOrder(treeNode.right);
            System.out.println(treeNode.val);
        }

        if (treeNode == null) return;

        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(treeNode);
        while (!stack1.isEmpty()) {
            TreeNode node = stack1.pop();
            // 要stack2出栈顺序时后序遍历 左右根  则入栈顺序为  根右左
            stack2.push(node);

            //stack2 入栈顺序为根右左 则stack1 入栈为左右
            if (node.left != null) {
                stack1.push(node.left);
            }

            if (node.right != null) {
                stack1.push(node.right);
            }
        }

        while (!stack2.isEmpty()) System.out.println(stack2.pop().val);

    }

}

